package top.huhuiyu.myforum.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import top.huhuiyu.myforum.dao.TbSubjectDAO;
import top.huhuiyu.myforum.dao.TbTopicDAO;
import top.huhuiyu.myforum.entity.TbSubject;
import top.huhuiyu.myforum.entity.TbTopic;
import top.huhuiyu.myforum.model.TbTopicModel;
import top.huhuiyu.myforum.util.JsonMessage;

/**
 * TbTopic的控制器类
 */
@RestController
@RequestMapping("/tbTopic")
public class TbTopicController {
	@Autowired
	private TbSubjectDAO tbSubjectDAO;
	@Autowired
	private TbTopicDAO tbTopicDAO;

	@RequestMapping("/save")
	public JsonMessage save(TbTopicModel tbTopicModel) throws Exception {
		// 测试模板 /tbTopic/save
		// {"tbTopic.sid":"1","tbTopic.tname":"3","tbTopic.description":"4"}
		tbTopicDAO.save(tbTopicModel.getTbTopic());
		return JsonMessage.getSuccessMessage("数据保存成功");
	}

	@RequestMapping("/update")
	public JsonMessage update(TbTopicModel tbTopicModel) throws Exception {
		// 测试模板 /tbTopic/update
		// {"tbTopic.tid":"1","tbTopic.sid":"2","tbTopic.isEnable":"3","tbTopic.tname":"4","tbTopic.description":"5"}
		tbTopicDAO.update(tbTopicModel.getTbTopic());
		return JsonMessage.getSuccessMessage("数据修改成功");
	}

	@RequestMapping("/delete")
	public JsonMessage delete(TbTopicModel tbTopicModel) throws Exception {
		// 测试模板 /tbTopic/delete {"tbTopic.tid":"1"}
		tbTopicDAO.delete(tbTopicModel.getTbTopic());
		return JsonMessage.getSuccessMessage("数据删除成功");
	}

	@RequestMapping("/queryByKey")
	public JsonMessage queryByKey(TbTopicModel tbTopicModel) throws Exception {
		// 测试模板 /tbTopic/queryByKey {"tbTopic.tid":"1"}
		TbTopic tbTopic = tbTopicDAO.queryByKey(tbTopicModel.getTbTopic());
		JsonMessage message = JsonMessage.getSuccessMessage("数据查询完成");
		message.putData("tbTopic", tbTopic);
		return message;
	}

	@RequestMapping("/queryAll")
	public JsonMessage queryAll(TbTopicModel tbTopicModel) throws Exception {
		// 测试模板 /tbTopic/queryAll {}
		List<TbSubject> subjects = tbSubjectDAO.queryAllNoPage();
		PageHelper.startPage(tbTopicModel.getPage().getPageNumber(), tbTopicModel.getPage().getPageSize());
		Page<TbTopic> list = (Page<TbTopic>) tbTopicDAO.queryAll();
		tbTopicModel.getPage().setPageInfo(list);
		JsonMessage message = JsonMessage.getSuccessMessage("查询成功！");
		message.putData("list", list);
		message.putData("page", tbTopicModel.getPage());
		message.putData("subjects", subjects);
		return message;
	}

}
